/**
 * Created by caosh on 2018/1/26 0026.
 */
var Details = new Vue({
    el: '#details',
    data: {
        details: {},
        isShowMask: false,
        isShowCart: false,
        cartBarTip: '编辑',
        selected: {},
        product: {},
        num: 0,
        popupVisible: false
    },
    methods: {
        goBack: function () {
            this.closeDetails();
            window.history.back(-1);
        },
        changeNorm: function (n) {
            this.selected = n
        },
        // 加
        plus: function () {
            this.num++;
        },
        // 减
        reduce: function () {
            this.num--;
        },
        clickPlus: function (p, n) {
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }
            this.changeMask(p, n);
            n.click = !n.click;
        },
        // 隐藏 / 显示 遮罩层
        changeMask: function (p, n) {
            this.isShowMask = !this.isShowMask;
            if (this.isShowMask) {
                this.product = this.getProductFromPool(p, n);
                this.product.unitPrice = n.price;
                this.product.unit = n.unit;
                this.product.nId = n.id;
                this.num = this.product.num;
                this.pId = p.id;
            } else {
                this.pId = 0;
            }
        },
        // 取消
        cancel: function () {
            this.num = this.product.num;
            this.changeMask();
        },
        // 确定
        confirm: function () {
            this.product.num = this.num;
            if (this.cartProducts.indexOf(this.product) == -1) {
                this.cartProducts.push(this.product);
            }
            // 本地缓存购物车数据
            // this.cacheCartProducts();
            this.changeMask();
        }
    },
    mounted: function () {
        this.details = store.get('details') || {};
        this.getCartProducts();
        this.selected = this.details.normList[0];
    },
    mixins: [CartMixin, ProductMixin, SystemMixin],
    watch: {
        num: function (val, oldVal) {
            if (val < 1) {
                this.num = 1;
            }
        },
        selected: {
            handler(newValue, oldValue) {
                this.popupVisible = newValue.unit==this.details.unit;
            },
            deep: true
        }
    }
});